<?php

function aasort($array, $key) {
    $sorter = array();
    $ret = array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii] = $va[$key];
    }
    arsort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii] = $array[$ii];
    }
    return $ret;
}

//aasort($your_array, "order");
$candidate_most_positive_score = aasort($candidate_list, 'sum_positive_score');
$candidate_most_wilson_score = aasort($candidate_list, 'sum_wilson_score');
$candidate_most_view_score = aasort($candidate_list, 'sum_view');
?>

<div class="row">
    <!-- Register Pane-->
    <div class="span8">
        <div class="is-block bottom">

            <ul id="myTab" class="nav nav-tabs">
                <li class="active"><a href="#positive-score" data-toggle="tab"><i class="icon-thumbs-up"></i> เรียงตามจำนวนโหวตชอบ</a></li>
                <li><a href="#view-score" data-toggle="tab"><i class="icon-eye-open"></i> เรียงตามจำนวนผู้ชม</a></li>
                <li><a href="#wilson-score" data-toggle="tab"><i class="icon-bolt"></i> เรียงตามจำนวนโหวตรวม</a></li>
            </ul>

            <div id="myTabContent" class="tab-content">
                <div class="tab-pane fade active in" id="positive-score">
                    <?php
                    foreach ($candidate_most_positive_score as $candidate) {
                        ?>
                        <div class="candidate_announce_wrapper">
                            <a href="<?php echo base_url() ?>contest/<?php echo $hash ?>/<?php echo $candidate['candidate_number'] ?>">
                                <img src="https://graph.facebook.com/<?php echo $candidate['fb_id'] ?>/picture" class="pull-left avatar img-polaroid img-rounded"/>
                                <div class="description pull-left">
                                    <?php echo '[#' . $candidate['candidate_number'] . '] - ' . $candidate['firstname'] . ' ' . $candidate['lastname'] ?><br/>
                                    <span><?php echo $candidate['title'] ?></span>
                                </div>
                                <div class="score pull-left">
                                    <i class="icon icon-thumbs-up"></i> <?php echo number_format($candidate['sum_positive_score']) ?>
                                </div>
                            </a>
                            <div class="clearfix"></div>
                        </div>
                        <?php
                    }
                    ?>
                </div>
                <div class="tab-pane fade active in" id="view-score">
                    <?php
                    foreach ($candidate_most_view_score as $candidate) {
                        ?>
                        <div class="candidate_announce_wrapper">
                            <a href="<?php echo base_url() ?>contest/<?php echo $hash ?>/<?php echo $candidate['candidate_number'] ?>">
                                <img src="https://graph.facebook.com/<?php echo $candidate['fb_id'] ?>/picture" class="pull-left avatar img-polaroid img-rounded"/>
                                <div class="description pull-left">
                                    <?php echo $candidate['firstname'] . ' ' . $candidate['lastname'] ?><br/>
                                    <span><?php echo $candidate['title'] ?></span>
                                </div>
                                <div class="score pull-left">
                                    <i class="icon icon-eye-open"></i> <?php echo number_format($candidate['sum_view']) ?>
                                </div>
                            </a>
                            <div class="clearfix"></div>
                        </div>
                        <?php
                    }
                    ?>
                </div>
                <div class="tab-pane fade active in" id="wilson-score">
                    <?php
                    foreach ($candidate_most_wilson_score as $candidate) {
                        ?>
                        <div class="candidate_announce_wrapper">
                            <a href="<?php echo base_url() ?>contest/<?php echo $hash ?>/<?php echo $candidate['candidate_number'] ?>">
                                <img src="https://graph.facebook.com/<?php echo $candidate['fb_id'] ?>/picture" class="pull-left avatar img-polaroid img-rounded"/>
                                <div class="description pull-left">
                                    <?php echo $candidate['firstname'] . ' ' . $candidate['lastname'] ?><br/>
                                    <span><?php echo $candidate['title'] ?></span>
                                </div>
                                <div class="score pull-left">
                                    <i class="icon icon-bolt"></i> <?php echo number_format($candidate['sum_wilson_score'], 2) ?>
                                </div>
                            </a>
                            <div class="clearfix"></div>
                        </div>
                        <?php
                    }
                    ?>
                </div>
            </div>

        </div>
    </div>
    <!-- รายละเอียดก่อนสมัคร -->
    <div class="span4">
        <div class="is-block bottom">
            <h5>เงื่อนไขการแข่งขันและการได้รับรางวัล</h5>

            <?php
            if ($campaign['candidate_vdo'] != 0) {
                echo '<i class="icon-film"></i> ส่งวีดีโอ Youtube จำนวน: ' . $campaign['candidate_vdo'] . ' คลิป';
            }
            if ($campaign['candidate_image'] != 0) {
                echo '<i class="icon-camera-retro"></i> ส่งรูปถ่ายจำนวน: ' . $campaign['candidate_image'] . ' รูป';
            }
            if ($campaign['candidate_text'] != 0) {
                echo '<i class="icon-file-alt"></i> ส่งเอกสารบทความจำนวน: ' . $campaign['candidate_text'] . ' ชิ้น';
            }
            ?>

            <?php
            $reward = $this->campaign->campaign_reward_title_display(
                    array(
                        'positive_score_reward' => $campaign['positive_score_reward'],
                        'view_score_reward' => $campaign['view_score_reward'],
                        'wilson_score_reward' => $campaign['wilson_score_reward'],
            ));
            if ($reward) {
                echo $reward;
            } else {
                echo 'ไม่มีรางวัลสำหรับการแข่งขันนี้';
            }
            ?>
            <hr/>
            <h5>จบการแข่งขันภายใน</h5>

            <?php
            echo $this->campaign->get_remain_day($campaign['stopped_at']);
            ?>
        </div>
    </div>
</div>
